package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.ConfirmCycleBillDto;
import com.csun.cmny.provider.dto.OmcOldLeaveDto;
import com.csun.cmny.provider.dto.OmcOldManBillDto;
import com.csun.cmny.provider.model.domain.OmcBillItem;
import com.csun.cmny.provider.model.domain.OmcOldManBill;
import com.csun.cmny.provider.model.dto.cycle.CycleBillDto;
import com.csun.cmny.provider.model.dto.cycle.OldmanFixedQueryDto;
import com.csun.cmny.provider.model.vo.GroupInforVo;
import com.csun.cmny.provider.model.vo.cycle.OldmanBillVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcOldManBillService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.validator.OmcOldLeaveValidator;
import com.csun.cmny.provider.vo.*;
import com.csun.cmny.util.DateUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzheng
 * @description:缴费记录单模块控制器
 * @date 2019/8/16 9:20
 */
@RestController
@RequestMapping(value = "/bill", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcOldManBillController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcOldManBillService omcOldManBillService;

    @Resource
    private UacRpcService uacRpcService;

    /**
     * 生成账单 -- 测试
     */
    @PostMapping("/createCycleBill")
    public Wrapper createCycleBill(@RequestBody CycleBillDto cycleBillDto) {

        omcOldManBillService.createCycleBill(cycleBillDto, getLoginAuthDto());

        return WrapMapper.ok();
    }

    /**
     * 生成账单 --
     */
    @PostMapping("/createCycleBillWithGroupId")
    public Wrapper createCycleBillWithGroupId(@RequestBody CycleBillDto cycleBillDto) {

        cycleBillDto.setCreateTime(DateUtil.strToDate(cycleBillDto.getCycleDate() + " 00:00:00"));
        omcOldManBillService.createCycleBillWithGroupId(cycleBillDto);

//        return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    /**
     * 根据账单查询应缴非项列表
     *
     * @param billId    账单Id
     *
     * @return
     */
    @PostMapping("/queryFixedListByBillId/{billId}")
    public Wrapper queryFixedListByBillId(@PathVariable Long billId) {

        List<OmcBillItem> omcBillItemList = omcOldManBillService.queryFixedListByBillId(billId);

//        return WrapMapper.ok(omcBillItemList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), omcBillItemList);
    }

    /**
     * 根据日期和老人Id集合查询账单应缴项
     *
     * @param oldmanFixedQueryDto
     *
     */
    @PostMapping("/queryFixedListByOldmanIdListAndDate")
    public Wrapper queryFixedListByOldmanIdListAndDate(@RequestBody OldmanFixedQueryDto oldmanFixedQueryDto) {

        oldmanFixedQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<OldmanBillVo> oldmanBillVoList = omcOldManBillService.queryFixedListByOldmanIdListAndDate(oldmanFixedQueryDto);

//        return WrapMapper.ok(oldmanBillVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), oldmanBillVoList);
    }

    /**
     * 确认账单
     *
     * @param confirmCycleBillDto
     *
     * @return
     */
    @PostMapping("/confirmCycleBill")
    public Wrapper confirmCycleBill(@RequestBody ConfirmCycleBillDto confirmCycleBillDto) {

        Integer result = omcOldManBillService.confirmCycleBill(confirmCycleBillDto);

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 分页查询账单信息
     * @param omcOldManBillDto
     * @return
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody OmcOldManBillDto omcOldManBillDto) {
        omcOldManBillDto.setGroupId(getLoginAuthDto().getGroupId());
        //查看已经通过审批的缴费单
        omcOldManBillDto.setIsPassed(1);
        PageHelper.startPage(omcOldManBillDto.getPageNum(), omcOldManBillDto.getPageSize());
        List<OmcOldManBillVo> list = omcOldManBillService.selectBillByName(omcOldManBillDto);
        PageInfo<OmcOldManBillVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), pageInfo);
    }

    /**
     * 查询待审批缴费单
     * @param omcOldManBillDto
     * @return
     */
    @RequestMapping(value = "/queryNeedApproveListWithPage", method = RequestMethod.POST)
    public Wrapper queryNeedApproveListWithPage(@RequestBody OmcOldManBillDto omcOldManBillDto) {
        omcOldManBillDto.setGroupId(getLoginAuthDto().getGroupId());
        //查看已经通过审批的缴费单
        PageHelper.startPage(omcOldManBillDto.getPageNum(), omcOldManBillDto.getPageSize());
        List<OmcOldManBillVo> list = omcOldManBillService.queryNeedApproveListWithPage(omcOldManBillDto);
        PageInfo<OmcOldManBillVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), pageInfo);
    }

    /**
     * 查询账单详情
     * @param omcOldManBillDto
     * @return
     */
    @RequestMapping(value = "/queryBillDetail", method = RequestMethod.POST)
    public Wrapper queryBillDetail(@RequestBody OmcOldManBillDto omcOldManBillDto ) {
//        if(null == omcOldManBillDto.getBillId() || 0 == omcOldManBillDto.getBillId()){
//            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("bill.id.is.not.null", null), null);
//        }
//        omcOldManBillDto.setGroupId(getLoginAuthDto().getGroupId());
//        OmcOldManBillDetailVo billDetailVo = omcOldManBillService.queryBillDetail(omcOldManBillDto);
//        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), billDetailVo);

        return WrapMapper.error("接口作废");
    }

    /**
     * 根据Id查询账单信息
     *
     * @param id    id
     *
     * @return  the bill
     */
    @RequestMapping(value = "/queryBillById/{id}", method = RequestMethod.POST)
    public Wrapper queryBillDetailById(@PathVariable Long id) {

        Object object = omcOldManBillService.queryBillById(id);

        return WrapMapper.ok(object);
    }

    /**
     * 查询账单详情,根据老人ID和账单类型
     * @param omcOldManBillDto
     * @return
     */
    @RequestMapping(value = "/queryBillByOldManId", method = RequestMethod.POST)
    public Wrapper queryBillByOldManId(@RequestBody OmcOldManBillDto omcOldManBillDto ) {
        omcOldManBillDto.setGroupId(getLoginAuthDto().getGroupId());
        if(null == omcOldManBillDto.getOldManId() || 0 == omcOldManBillDto.getOldManId()){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("oldman.id.null", null), null);
        }
        omcOldManBillDto.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcOldManBillDetailVo> list = omcOldManBillService.queryBillDetailList(omcOldManBillDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

    /**
     * 账单缴费
     * @param omcOldManBillDto
     * @return
     */
    @RequestMapping(value = "/payFee", method = RequestMethod.POST)
    public Wrapper payFee(@RequestBody OmcOldManBillDto omcOldManBillDto ) {
        if(null == omcOldManBillDto.getBillId() || 0 == omcOldManBillDto.getBillId()){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("bill.id.is.not.null", null), null);
        }
        omcOldManBillDto.setGroupId(getLoginAuthDto().getGroupId());
        boolean result = omcOldManBillService.payFee(omcOldManBillDto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 院长审批缴费单
     * @param: [billId]queryListWithPage
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @RequestMapping(value = "/approveByAdmin/{billId}/{isPassed}", method = RequestMethod.POST)
    public Wrapper approveByAdmin(@PathVariable Long billId ,@PathVariable Integer isPassed) {
        if(null == billId || 0 == billId){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("bill.id.is.not.null", null), null);
        }
        if(null == isPassed){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("isPassed.is.null", null), null);
        }
        if(isPassed != 1 && isPassed != 0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("isPassed.is.error", null), null);
        }
        int ret = omcOldManBillService.approveByAdmin(billId,isPassed,getLoginAuthDto());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /*
    * 集团化缴费记录单查询
    * */
    @PostMapping("/selectAllBill")
    public Wrapper selectAllBill(@RequestBody OldmanBillInforVo oldmanbill){
        oldmanbill.setGroupId(getLoginAuthDto().getGroupId());
        PageHelper.startPage(oldmanbill.getPageNum(), oldmanbill.getPageSize());
        List<OldmanBillInforVo> oldmanBillInforVos = omcOldManBillService.selectAllBill(oldmanbill);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(oldmanBillInforVos));
    }

    /*
    * 营业额报表查询
    * */
    @PostMapping("/selectTurnover")
    public Wrapper selectTurnover(@RequestBody TurnoverVo turnoverVo){
        List<GroupInforVo> allGroup = uacRpcService.getAllGroup(getLoginAuthDto().getGroupId());
        GroupInforVo groupInforVo = new GroupInforVo();
        groupInforVo.setGroupName("合计");
        allGroup.add(groupInforVo);
        turnoverVo.setGroupId(getLoginAuthDto().getGroupId());
        List<TurnoverVo> turnoverVos = omcOldManBillService.selectTurnover(turnoverVo);
        ArrayList<TurnoverVo> turnoverList = new ArrayList<>();
        for (GroupInforVo g:allGroup) {
            TurnoverVo tu = new TurnoverVo();
            tu.setGroupId(g.getId());
            tu.setGroupName(g.getGroupName());
            for (TurnoverVo t:turnoverVos) {
                if(t.getGroupName().equals(tu.getGroupName())){
                    tu.setMonths(t.getMonths());
                }
            }
            turnoverList.add(tu);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), turnoverList);
    }

}
